<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@page import="com.google.appengine.api.users.User"%>
<%@page import="it.cpyx.server.db.utils.TextDataUtils"%>
<%@page import="java.util.List"%>
<%@page import="it.cpyx.server.db.TextData"%>
<%@page import="javax.jdo.PersistenceManager"%>
<%@page import="it.cpyx.server.db.utils.PMF"%>
<%@page import="java.util.Iterator"%>
<%@page import="it.cpyx.server.search.SearchJanitor"%>
<html>
	<head>
		<meta name="google-site-verification" content="-rdtDYfVJU6Pafik3SPvPeMKsvX9PyboVpVPI5TQ44g" />
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
		<title>CpyX.IT Easy Way to Share Your Texts</title>
	</head>
	<body>
	BETA!!<br>
		<script type="text/javascript">
	
	  var _gaq = _gaq || [];
	  _gaq.push(['_setAccount', 'UA-5921292-11']);
	  _gaq.push(['_trackPageview']);
	
	  (function() {
	    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	  })();
	
	</script>
		<a href="/">home</a>
		<hr>
		<form action="/sh" method="get">
      <div><input name="q"></input></div>
      <div><input type="submit" value="Search" /></div>
    </form>
    <hr>


<%
	String searchString = request.getParameter("q");


if (searchString != null) {
	
	out.println("Results for:<b> " + searchString + "</b>");
	   PersistenceManager pm = PMF.get().getPersistenceManager();
	   
		
		List<TextData> searchResults = TextDataUtils.searchTextDataByContentString(searchString,pm);
	
	
    if (searchResults.isEmpty()) {
%>
    	<p>No results found. Please try different search.</p>
    	<%
    		} else { int i=1;
    	    	        for (TextData g : searchResults) {

    	%><hr><%=i%>:
    	<blockquote><%= g.getContents().getValue() %></blockquote>
    	<%				i++;
    	        }
    	    }
    	    pm.close();
}

%>
<hr>

<%
	UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
%>
		<p>Hello, <%=user.getNickname()%>! (You can <a href="<%=userService.createLogoutURL(request.getRequestURI())%>">sign out</a>.)</p>
<%
	} else {
%>
		<p>Hello! <a href="<%=userService.createLoginURL(request.getRequestURI())%>">Sign in</a>.</p>
<%
	}
%><hr>
	
		index<%=request.getPathInfo()%>
		<hr>
	    <form action="/s" method="post">
	    	<div>text:</div>
	    	<div><textarea  name="content" rows="20" cols="100"></textarea></div>
	    	<div>//optional</div>
	    	<div>Title:</div>
	    	<div><input name="title"/></div>
	    	<div>pass:</div>
	    	<div><input name="password"/></div>
	    	<div>Custom alias:</div>
	    	<div><input name="manualId"/></div>
	    	<div><input type="submit" value="CpyX.IT" /></div>

	    </form>
	    <hr>
	    
    </body>

</html>